package cn.tj.wenda.cloud.util;

import java.lang.reflect.Method;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.pagehelper.PageInfo;

import cn.tj.wenda.cloud.entity.LoginUserInfo;
import cn.tj.wenda.cloud.entity.ResponseBean;

/**
 * 实体类处理工具
 * @author Wenda
 * @date 2018年9月28日
 */
public class EntityUtil {
	
	private static Logger logger = LoggerFactory.getLogger(EntityUtil.class);
	
	/***
	 * 为实体对象填充 adduserid，adduser，addtime，tenantid信息，若不存在则不填充
	 * @param <T>
	 * @param entity
	 * @param loginUserInfo
	 * @return
	 */
	public <T> InnerUpd<T> addAddUserInfo(T entity,LoginUserInfo loginUserInfo) {
		Class<? extends Object> cls = entity.getClass();
		try {
			this.setEntityValue(entity,cls,"adduserid",loginUserInfo.getUserId());
			this.setEntityValue(entity,cls,"adduser",loginUserInfo.getUserName());
			this.setEntityValue(entity,cls,"addtime",DateTime.getCurrDateTime());
			this.setEntityValue(entity,cls,"tenantid",loginUserInfo.getTenantId());
		}
		catch(Exception e) {
			logger.error("为实体" + cls.getName() + "添加默认列出错：",e);
		}
		
		return new InnerUpd<T>(entity,loginUserInfo);
	}
		
	/***
	 * 为实体对象填充 upduserid,upduser,updtime，若不存在则不填充
	 * @param entity
	 * @param loginUserInfo
	 * @return
	 */
	public <T> InnerAdd<T> addUpdUserInfo(T entity,LoginUserInfo loginUserInfo) { 
		Class<? extends Object> cls = entity.getClass();
		try {
			this.setEntityValue(entity,cls,"upduserid",loginUserInfo.getUserId());
			this.setEntityValue(entity,cls,"upduser",loginUserInfo.getUserName());
			this.setEntityValue(entity,cls,"updtime",DateTime.getCurrDateTime());
		}
		catch(Exception e) {
			logger.error("为实体" + cls.getName() + "修改默认列出错：",e);
		}
		return new InnerAdd<T>(entity,loginUserInfo);
	}

	public class InnerAdd<T>{
		/***
		 * 为实体对象填充 adduserid，adduser，addtime，tenantid信息，若不存在则不填充
		 */
		private T entity = null;
		private LoginUserInfo loginUserInfo = null;
		public InnerAdd(T entity,LoginUserInfo loginUserInfo) {
			this.entity = entity;
			this.loginUserInfo = loginUserInfo;
		}
		public void addAddUserInfo(){ 
			EntityUtil.this.addAddUserInfo(this.entity,this.loginUserInfo);
		}
    }
	
	public class InnerUpd<T>{
		/***
		 * 为实体对象填充 upduserid,upduser,updtime，若不存在则不填充
		 */
		private T entity = null;
		private LoginUserInfo loginUserInfo = null;
		
		public InnerUpd(T entity,LoginUserInfo loginUserInfo) {
			this.entity = entity;
			this.loginUserInfo = loginUserInfo;
		}
		
        public void addUpdUserInfo(){ 
        	EntityUtil.this.addUpdUserInfo(entity,loginUserInfo);
        } 
    }
	
	/***
	 * 自动为ResponseJsonBean附加Data和分页信息
	 * @param res ResponseJsonBean
	 * @param lstResult 查询结果List
	 */
	public <T> ResponseBean addPageInfo(ResponseBean res,List<T> lstResult) {
		PageInfo<T> pageInfo = new PageInfo<T>(lstResult);
		res.setData(pageInfo.getList());
		res.setPageCount(pageInfo.getPages());
		res.setPageIndex(pageInfo.getPageNum());
		res.setPageSize(pageInfo.getPageSize());
		res.setRecordCount((int) pageInfo.getTotal());
		return res;
	}
	
	/**
	 * 自动建立ResponseJsonBean并为ResponseJsonBean附加Data和分页信息
	 * @param lstResult
	 * @return
	 */
	public <T> ResponseBean addPageInfo(List<T> lstResult) {
		return this.addPageInfo(new ResponseBean(), lstResult);
	}

	/***
	 * 自动为ResponseJsonBean附加Data和分页信息
	 * @param res ResponseJsonBean
	 * @param pageInfo 查询结果pageInfo
	 */
	public <T> ResponseBean addPageInfo(ResponseBean res,PageInfo<T> pageInfo) {
		res.setData(pageInfo.getList());
		res.setPageCount(pageInfo.getPages());
		res.setPageIndex(pageInfo.getPageNum());
		res.setPageSize(pageInfo.getPageSize());
		res.setRecordCount((int) pageInfo.getTotal());
		return res;
	}
	
	/**
	 * 自动建立ResponseJsonBean并为ResponseJsonBean附加Data和分页信息
	 * @param pageInfo
	 * @return
	 */
	public <T> ResponseBean addPageInfo(PageInfo<T> pageInfo) {
		return this.addPageInfo(new ResponseBean(), pageInfo);
	}

	private <T>  void setEntityValue(T entity,Class<? extends Object> cls,String fieldName,String val) throws Exception {
		String name = String.format("set%s%s", fieldName.substring(0,1).toUpperCase(),fieldName.substring(1));
		Method method = cls.getMethod(name,String.class);
		if(method != null) {
			method.invoke(entity,val);
		}
	}
}
